package com.atguigu.yygh.hosp.controller.api;


import com.atguigu.yygh.common.exceptionhandle.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {


    private final HospitalService hospitalService;

    private final HospitalSetService hospitalSetService;

    private final DepartmentService departmentService;

    private final ScheduleService scheduleService;

    @Autowired
    public ApiController(HospitalService hospitalService, HospitalSetService hospitalSetSerlvice, DepartmentService departmentService, ScheduleService scheduleService) {
        this.hospitalService = hospitalService;
        this.hospitalSetService = hospitalSetSerlvice;
        this.departmentService = departmentService;
        this.scheduleService = scheduleService;
    }

    /**
     * 上传医院接口
     */
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 获取传递过来的医院信息
        // 将string[]转为object类
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 先判断你这个医院是否有sign才能进行添加
        String sign = (String) paramMap.get("sign");
        // sign进行了md5加密

        // 根据传递过来的医院编号 查询sign是否一样
        String hoscode = (String) paramMap.get("hoscode");

        String signKey = hospitalSetService.getSignKey(hoscode);

        // 根据数据库的签名进行加密
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)) {
            throw new YyghException("签名异常", 20001);
        }

        // base64原本的+会变为空格
        String logoData = (String) paramMap.get("logoData");

        logoData = logoData.replaceAll(" ", "+");
        // 进行覆盖
        paramMap.put("logoData", logoData);

        // 将map集合得到进行添加
        // 调用service方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    /**
     * 查询医院接口
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        // 获取传递过来的医院信息
        // 将string[]转为object类
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 先判断你这个医院是否有sign才能进行添加
        String sign = (String) paramMap.get("sign");
        // sign进行了md5加密

        // 根据传递过来的医院编号 查询sign是否一样
        String hoscode = (String) paramMap.get("hoscode");

        String signKey = hospitalSetService.getSignKey(hoscode);

        // 根据数据库的签名进行加密
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)) {
            throw new YyghException("签名异常", 20001);
        }

        // 调用service实现根据医院编号查询
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);

        return Result.ok().data("data", hospital);
    }

    /**
     * 上传科室接口
     */
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        // 先判断你这个医院是否有sign才能进行添加
        String sign = (String) paramMap.get("sign");
        // sign进行了md5加密

        String signKey = hospitalSetService.getSignKey(hoscode);

        // 根据数据库的签名进行加密
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)) {
            throw new YyghException("签名异常", 20001);
        }

        departmentService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        // 先判断你这个医院是否有sign才能进行添加
        String sign = (String) paramMap.get("sign");
        // sign进行了md5加密

        String signKey = hospitalSetService.getSignKey(hoscode);

        // 根据数据库的签名进行加密
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)) {
            throw new YyghException("签名异常", 20001);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok().data("data", pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        // 先判断你这个医院是否有sign才能进行添加
        String sign = (String) paramMap.get("sign");
        // sign进行了md5加密

        String signKey = hospitalSetService.getSignKey(hoscode);

        // 根据数据库的签名进行加密
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)) {
            throw new YyghException("签名异常", 20001);
        }

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));


        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        // 先判断你这个医院是否有sign才能进行添加
        String sign = (String) paramMap.get("sign");
        // sign进行了md5加密

        String signKey = hospitalSetService.getSignKey(hoscode);

        // 根据数据库的签名进行加密
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)) {
            throw new YyghException("签名异常", 20001);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page, limit, scheduleQueryVo);
        return Result.ok().data("data", pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        // 根据医院编号与排班编号删除科室
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");

        //必填
        String hosScheduleId = (String) paramMap.get("hosScheduleId");


        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        // 先判断你这个医院是否有sign才能进行添加
        String sign = (String) paramMap.get("sign");
        // sign进行了md5加密

        String signKey = hospitalSetService.getSignKey(hoscode);

        // 根据数据库的签名进行加密
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)) {
            throw new YyghException("签名异常", 20001);
        }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");

        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        // 先判断你这个医院是否有sign才能进行添加
        String sign = (String) paramMap.get("sign");
        // sign进行了md5加密

        String signKey = hospitalSetService.getSignKey(hoscode);

        // 根据数据库的签名进行加密
        String signKeyMD5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMD5)) {
            throw new YyghException("签名异常", 20001);
        }

        scheduleService.save(paramMap);
        return Result.ok();

    }
}
